package org.sunsys.mvc.example1;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.sunsys.mvc.example1.service.HomePageService;


@Controller
public class HomePageController {

	private HomePageService homePageService = null;
	
	public HomePageController() {
	}

	/*
	 * Difference between @Inject      and @Autowire
	 * 
	 * @Inject is part of Java Context and Dependency Inject (Java CDI) specification
	 * @Autowire is Spring specific annotation
	 * 
	 * Spring supports Java CDI, hence we can use @Inject in spring applications
	 */
	public HomePageController(HomePageService homePageService){
		this.homePageService = homePageService;
	}
	
	public HomePageService getHomePageService() {
		return homePageService;
	}
	
	@RequestMapping(value={"/", "/home"}, method=RequestMethod.GET )
	public String showHomePage(){
		System.out.println("showHomePage method");
		return "/homePage";
	}
	
	@RequestMapping(value={"/home"}, method=RequestMethod.GET, params="userName" )
	public String showHomePage2(@RequestParam("userName") String userName){
		/*
		 * @RequestParam("userName") reads the userName attribute from request URL/form 
		 * same will be assigned to userName
		 */
		/*
		 * if we specify the params in @RequestMapping, the method will be called, only with url has the specified params's value
		 */
		System.out.println("showHomePage method with param");
		return "redirect:/homePage.html";
	}
	
}
